package itheima.d03;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws Exception {
        ArrayList<User> list = new ArrayList<>();


        SAXReader reader = new SAXReader();
        Document doc = reader.read("D:\\javabasepro\\java AdvanceProject\\day12\\src\\itheima\\d03\\users.xml");

        Element users = doc.getRootElement();


        List<Element> userElements = users.elements("user");

        for (Element userElement : userElements) {
            int age= Integer.parseInt(userElement.elementText("age"));
            String sex =userElement.elementText("sex");
            String address =userElement.elementText("address");

            Element nameEle = userElement.element("name");
            String name = nameEle.getText();
            int num =Integer.parseInt(nameEle.attributeValue("num"));
            int id =Integer.parseInt(nameEle.attributeValue("id"));
            User user = new User(id,num,name,age,sex,address);
            list.add(user);
        }
        System.out.println("解析完毕");

        list.forEach(System.out::println);

    }
}
